package com.dayuanit.dy15.atm.service.impl;

import com.dayuanit.dy15.atm.dto.UserDetailDTO;
import com.dayuanit.dy15.atm.entity.UserDetail;
import com.dayuanit.dy15.atm.enums.EmailStatusEnum;
import com.dayuanit.dy15.atm.exception.BizException;
import com.dayuanit.dy15.atm.mapper.UserDetailMapper;
import com.dayuanit.dy15.atm.service.UserDeailService;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UserDeailServiceImpl implements UserDeailService {

    @Autowired
    private UserDetailMapper userDetailMapper;

    @Value("${salt.value}")
    private String saltValue;

    @Override
    public void bindEamil(String email, long userId) {

        System.out.println("service saltValue=" + saltValue);

        if (StringUtils.isBlank(email)) {
            throw new BizException("邮箱不能为空");
        }

        UserDetail userDetail = userDetailMapper.getByUserId(userId);
        if (userDetail != null && userDetail.getStatus() == EmailStatusEnum.activate.getK()) {
            throw new BizException("已绑定邮箱");
        }

        UserDetail checkEmailDetail = userDetailMapper.getByEmail(email);

        //校验email地址
        if (checkEmailDetail != null && checkEmailDetail.getStatus() == EmailStatusEnum.activate.getK()) {
            throw new BizException("邮箱已激活");
        }

        if (checkEmailDetail != null && checkEmailDetail.getUserId() == userId) {
            return;
        }

        if (userDetail != null && email.equals(userDetail.getEmail())) {
            return;
        }

        if (userDetail != null && !email.equals(userDetail.getEmail())) {
            userDetail.setEmail(email);
            int rows = userDetailMapper.updateByPrimaryKey(userDetail);
            return;
        }

        userDetail = new UserDetail();
        userDetail.setCreateTime(new Date());
        userDetail.setEmail(email);
        userDetail.setModifyTime(userDetail.getCreateTime());
        userDetail.setStatus(EmailStatusEnum.un_activate.getK());
        userDetail.setUserId(userId);

        userDetailMapper.insert(userDetail);
    }

    @Override
    public void activeEmail(String email, long expireTime, String sign) {
        if (StringUtils.isBlank(email)) {
            throw new BizException("邮箱不能为空");
        }

        if (System.currentTimeMillis() > expireTime) {
            System.out.println("激活请求过期了，请重新绑定");
            throw new BizException("激活失败");
        }

        //明文的组成规则=email + 随机盐值 + 过期时间(毫秒数)
        //明文一样的  得到的签名也是一样的  md5 sha消息摘要算法是不可逆的
        String msg = email + saltValue + expireTime;
        String tmpSign = DigestUtils.md5Hex(msg);

        if (!tmpSign.equals(sign)) {
            throw new BizException("激活失败");
        }

        UserDetail userDetail = userDetailMapper.getByEmail(email);

        if (userDetail == null) {
            throw new BizException("非法邮箱");
        }

        if (userDetail.getStatus() == EmailStatusEnum.activate.getK()) {
            throw new BizException("请勿重新激活");
        }

        userDetail.setStatus(EmailStatusEnum.activate.getK());
        userDetail.setModifyTime(new Date());
        int rows = userDetailMapper.updateByPrimaryKey(userDetail);

        if (1 != rows) {
            throw new BizException("邮箱激活失败");
        }
    }

    @Override
    public UserDetailDTO checkBindEmail(long userId) {
        UserDetail userDetail = userDetailMapper.getByUserId(userId);
        if (null == userDetail) {
            return new UserDetailDTO("", EmailStatusEnum.un_activate.getK());
        }
        return new UserDetailDTO(userDetail.getEmail(), userDetail.getStatus());
    }

    @Override
    public void unBindEmail(long userId) {
        UserDetail userDetail = userDetailMapper.getByUserId(userId);
        if (userDetail == null || userDetail.getStatus() != EmailStatusEnum.activate.getK()) {
            throw new BizException("邮箱未激活");
        }

        userDetail.setStatus(EmailStatusEnum.un_activate.getK());
        userDetail.setModifyTime(new Date());

        int rows = userDetailMapper.updateByPrimaryKey(userDetail);
        if (1 != rows) {
            throw new BizException("解绑失败");
        }
    }

    @Override
    public UserDetailDTO checkBindEmail(String email) {
        UserDetail userDetail = userDetailMapper.getByEmail(email);
        if (null == userDetail || userDetail.getStatus() != EmailStatusEnum.activate.getK()) {
            throw new BizException("邮箱未激活");
        }

        return new UserDetailDTO(userDetail.getEmail(), userDetail.getStatus());
    }
}
